package cn.schoolwow.quickdao.test.module.business.user.dml.json.flow.edit;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Assert;

public class UpdateUserJSONByIdProcessFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        singleUpdate(flowContext);
        multipleUpdate(flowContext);
    }

    @Override
    public String name() {
        return "根据用户id更新用户-JSON格式";
    }

    private void singleUpdate(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        JSONObject insertUser = (JSONObject) flowContext.checkData("insertUser");

        insertUser.put("password", "111111");
        int effect = dao.uniqueFieldNames("id").update("user", insertUser);
        Assert.assertEquals(1, effect);
    }

    private void multipleUpdate(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        JSONArray insertUserList = (JSONArray) flowContext.checkData("insertUserList");

        for(int i=0;i<insertUserList.size();i++){
            JSONObject insertUser = insertUserList.getJSONObject(i);
            insertUser.put("password", "654321");
        }

        int effect = dao.uniqueFieldNames("id").update("user", insertUserList);
        Assert.assertEquals(insertUserList.size(), effect);
    }
}
